

#include "main.h"

#define   RF_INPUTS_OFFSET		0x80 /* name which great(or equal) than 0x80 for RF inputs only */

unsigned char RF_Message[RF_BUFFER_SIZE];

near bit RF_MESSAGE_RECEIVED;

#define   RF_REMOTE_BUTTON_ARM		8
#define   RF_REMOTE_BUTTON_DISARM	4
#define   RF_REMOTE_BUTTON_PANIC		2

near unsigned char RF_LearnParameters;

unsigned char RF_Errors;
unsigned long rf_key;


// ISV 255 2 20 [1]INPvi
void RF_Manager ( void )
{
	static unsigned char State = 0;
	RECORD_RF_REMOTE Received_RF_Remote;
	
	if ( RF_MESSAGE_RECEIVED )
	{
		rf_key = 0;
		MemCpy((unsigned char*)&rf_key, RF_Message, RF_BUFFER_SIZE);
		
		dbg("\nrf_key: %l\n", rf_key); //Dima
		
		if ( Execute_Module_1 != RF_LearMode )
		{
			RC_PressedKey = rf_key;
		}
	}
}

/* Total ROM used    29600 bytes (22.6%)
	Total RAM used     2019 bytes (52.6%)   Near RAM used       42 bytes (43.8%)
 */

void RF_Get_Button ( RECORD_RF_REMOTE* Record_RF_Remote )
{
	unsigned char i, tmp;

	tmp = ~(Record_RF_Remote -> Button);
   Record_RF_Remote -> Button = 0;
	for( i =1; i < 9; i++)
	{
		if ( i & 0x01 )
		{
   		 Record_RF_Remote -> Button <<=1;   
			if ( tmp & 0x01 )
				Record_RF_Remote -> Button |= 1;
   	  
   	}

		tmp >>= 1;
	}
}

unsigned long RF_Get_RemoteNumber ( void )
{
	unsigned long Number = 0;
	MemCpy((unsigned char*)&Number, RF_Message, 3);

	/* if ( RF_LearnParameters == RF_REMOTE_TRANSMIT ) */
	/* { */
	/* 	MemCpy((unsigned char*)&Number, RF_Message, 3); */
	/* } */
	/* else */
	/* { */
	/* 	MemCpy((unsigned char*)&Number, RF_Message, 2); */
	/* } */

	Debug("\rRF Remote Number: %d", Number);
	return Number;
} 

/* unsigned char RF_Input_Handler ( unsigned char Input ) */
/* { */
/* 	static unsigned char State = 0, Timeout = 5, MessageNumber = 0; */

/* 	static RF_INPUT_DESCRIPTOR RF_Input_Descriptor; */

/* 	if ( Time_1000ms && --Timeout == 0 ) */
/* 	{ */
/* 		State = 0; */
/* 		Timeout = 5; */
/* 		RF_Errors = TIMEOUT__RF_Input_Handler; */
/* 		return State; */
/* 	} */

/* 	switch( State ) */
/* 	{ */
/* 		case 0: */
/* 			RF_Input_Descriptor.Button = Input; */

/* 			if ( Ex_Entry_Read(LIST_RF_INPUTS_DESCRIPTORS, (unsigned char*)&RF_Input_Descriptor ) != EX_ERROR ) */
/* 			{ */
/* 				switch ( RF_Input_Descriptor.OutputNumber ) */
/* 				{ */
/* 					case 1: */
/* 						OUT_1 = RF_Input_Descriptor.OutputType; */
/* 						break; */


/* 					case 2: */
/* 						OUT_2 = RF_Input_Descriptor.OutputType; */
/* 						break; */

/* 					default: */
/* 						; */
/* 				} */
/* 				State++; */
/* 			} */
/* 			else */
/* 			{ */
/* 				Debug("RF_Input(0x%X) is not defined", RF_Input_Descriptor.OutputNumber); */
/* 				RF_Errors = RF_INPUT_IS_NOT_DEFINED; */
/* 			} */
/* 			break; */


/* 		case 1: */
/* 			if ( SMS_Sender_Manager__Status == READY  ) */
/* 			{ */
/* 				if ( Ex_Entry_Read(LIST_RF_INPUTS_DESCRIPTORS, (unsigned char*)&RF_Input_Descriptor) == EX_ERROR ) */
/* 				{ */
/* 					Debug(">>GET LIST_RF_INPUTS_DESCRIPTORS(%d): ERROR ", RF_Input_Descriptor.Button); */
/* 					State = 3; */
/* 					break; */
/* 				} */

/* 				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(StrToLong(RF_Input_Descriptor.Message[MessageNumber])); */

/* 				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP ) */
/* 				{ */
/* 					Debug(">>RF ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo);  */
/* 					State = 3; */
/* 					break; */
/* 				} */
				
/* 				Timeout = 10; */
/* 				//StrCpy(SMS_Sender_Manager__Message, RF_Input_Descriptor.Message[MessageNumber]); */
/* 				Write_SMS(RF_Input_Descriptor.Message[MessageNumber]); */
				
/* 				putsf("\rSMS Message: ", DEBUG_PORT); */
/* 				puts(SMS_Sender_Manager__Message, DEBUG_PORT); */
				
/* 				if ( ++MessageNumber > RF_INPUT_DESCRIPTOR_MESSAGES_COUNT ) */
/* 				{ */
/* 					State = 3; */
/* 				} */
/* 				else */
/* 				{ */
/* 					SMS_Sender_Manager__Status = BUSY; */
/* 					Tools = SMS_Sender_Manager; */
/* 				} */
/* 			} */
/* 			break; */

/* 		case 3: */
/* 			MessageNumber = 0; */
/* 			State = 0; */
/* 			break; */

/* /\* 			if ( SMS_Sender_Manager__Status == READY ) *\/ */
/* /\* 			{ *\/ */
/* /\* 				SMS_Sender_Manager__Status = BUSY; *\/ */
/* /\* 				SMS_Sender_Manager__SendTo = LIST_OWNERS; *\/ */
/* /\* 				StrCpy(SMS_Sender_Manager__Message, RF_Input_Descriptor.Message[0]);   *\/ */
/* /\* 				Tools = SMS_Sender_Manager; *\/ */
/* /\* 				Timeout = 5; *\/ */
/* /\* 				State = 0; *\/ */
/* /\* 			} *\/ */

/* /\* 			break; *\/ */

/* 		default: */
/* 			; */
/* 	} */

/* 	return State; */
/* } */

/* void Drive_RF_Input ( void ) */
/* { */
/* 	static unsigned char Timer = 0; */

/* 	RF_MESSAGE_RECEIVED = FALSE; */

/* 	if ( Timer ) */
/* 	{ */
/* 		if ( Time_10ms ) */
/* 		{ */
/* 			Timer--; */
/* 		} */
/* 	} */

/* 	if ( RF_Complete ) */
/* 	{ */
/* 		if ( Timer == 0 ) */
/* 		{  */
/* //			PIN_LED ^= 1; */
/* 			Timer = 10; */
/* 			MemCpy(RF_Message, RF_RecBuffer, RF_BUFFER_SIZE); */
/* 			Debug("\rRF_Message[0]: %X", RF_RecBuffer[0]); */
/* 			Debug("\rRF_Message[1]: %X", RF_RecBuffer[1]); */
/* 			Debug("\rRF_Message[2]: %X", RF_RecBuffer[2]); */
/* 			RF_MESSAGE_RECEIVED = TRUE; */
/* 		} */
/* 		else */
/* 		{ */
/* 			Timer = 10; */
/* 		} */
	
/* 		ReceiverEna(); */
/* 	} */

/* } */

unsigned char mem_RF_RecBuffer[3];

/* void Drive_RF_Input ( void ) */
/* { */
/* 	//static unsigned char Timer = 0; */
/* //	static unsigned char t = 0; */
	
/* 	RF_MESSAGE_RECEIVED = FALSE; */
/* 	if ( RF_Complete ) */
/* 	{ */
/* 		Debug("\r%X ", RF_RecBuffer[0]); */
/* 		Debug("%X ", RF_RecBuffer[1]); */
/* 		Debug("%X", RF_RecBuffer[2]); */
/* 		RF_RecBuffer[2] = 0; */

/* 		ReceiverEna(); */
/* 	} */

/* } */

void Drive_RF_Input ( void )
{
	//static unsigned char Timer = 0;
	static unsigned char t = 0;
	
	RF_MESSAGE_RECEIVED = FALSE;

/* 	if ( Timer ) */
/* 	{ */
/* 		if ( Time_10ms ) */
/* 		{ */
/* 			Timer--; */
/* 		} */
/* 	} */

	if ( RF_Complete )
	{
		//dbg("RRRRRRRRRRRRRRRRRR"); //Dima
		if ( t )
		{
			if ( mem_RF_RecBuffer[0] == RF_RecBuffer[0] &&
				  mem_RF_RecBuffer[1] == RF_RecBuffer[1] &&
				  mem_RF_RecBuffer[2] == RF_RecBuffer[2] )
			{
				Debug("\r%X ", RF_RecBuffer[0]);
				Debug("%X ", RF_RecBuffer[1]);
				Debug("%X", RF_RecBuffer[2]);
				
				RF_MESSAGE_RECEIVED = TRUE;
				MemCpy(RF_Message, RF_RecBuffer, RF_BUFFER_SIZE);
			}
			
			t = 0;

		}
		else
		{
			mem_RF_RecBuffer[0] = RF_RecBuffer[0];
			mem_RF_RecBuffer[1] = RF_RecBuffer[1];
			mem_RF_RecBuffer[2] = RF_RecBuffer[2];
			t++;
		}


		RF_RecBuffer[0] = 0;
		RF_RecBuffer[1] = 0;
		RF_RecBuffer[2] = 0;

		ReceiverEna();
	}

}


extern void CommandHandler_IS ( void );
extern unsigned long rf_inp;

void RF_LearMode ( void )
{
	static unsigned char State = 0, Timeout = 10;
	static unsigned long FirstTimeNumber, SecondTimeNumber; 
	unsigned int Address;
	RECORD_RF_REMOTE Record_RF_Remote;

	
	if ( rf_inp != 0 || (Time_1000ms && --Timeout == 0) )
	{ 
		State = 0;
		SysState = IDLE;
		Timeout = 10;
		RF_LearnParameters = 0;
		Execute_Module_1 = CommandHandler_IS;
		putsf("\r============ END OF RF LEARN MODE ===========\r", AUX_PORT);
		return;
	}


	switch ( State )
	{
		case 0:
			putsf("\r\t\t>> Press BUTTON\r", AUX_PORT);
			State++;

		case 1:
			if ( RF_MESSAGE_RECEIVED )
			{
				FirstTimeNumber = RF_Get_RemoteNumber();
				
				putsf("\r\t\t>> Press BUTTON again\r", AUX_PORT);
				Timeout = 8;
				State++;
			}
			break;


		case 2:
			if ( RF_MESSAGE_RECEIVED )
			{
				SecondTimeNumber = RF_Get_RemoteNumber();
				Timeout = 8;
				
				if ( FirstTimeNumber == SecondTimeNumber )
				{
					rf_inp = SecondTimeNumber;
					Debug("\r\t\t>>> RF - OK; ", Address );
				}
				else
				{
					putsf("\r\t\t>> ERROR: is not the SAME BUTTON\r \t\tTry again.\r", AUX_PORT);
				}

				State = 0;
			}
			break;

		default:
			;
	}

} 


/* void ___RF_LearMode ( void ) */
/* { */
/* 	static unsigned char State = 0, Timeout = 10; */
/* 	static unsigned long FirstTimeNumber, SecondTimeNumber;  */
/* 	unsigned int Address; */
/* 	RECORD_RF_REMOTE Record_RF_Remote; */

	
/* 	if ( Time_1000ms && --Timeout == 0 ) */
/* 	{  */
/* 		State = 0; */
/* 		SysState = IDLE; */
/* 		Timeout = 10; */
/* 		RF_LearnParameters = 0; */
/* 		putsf("\r============ END OF RF LEARN MODE ===========\r", AUX_PORT); */
/* 		return; */
/* 	} */


/* 	switch ( State ) */
/* 	{ */
/* 		case 0: */
/* 			putsf("\r\t\t>> Press BUTTON\r", AUX_PORT); */
/* 			State++; */

/* 		case 1: */
/* 			if ( RF_MESSAGE_RECEIVED ) */
/* 			{ */
/* 				FirstTimeNumber = RF_Get_RemoteNumber(); */
				
/* 				putsf("\r\t\t>> Press BUTTON again\r", AUX_PORT); */
/* 				Timeout = 8; */
/* 				State++; */
/* 			} */
/* 			break; */


/* 		case 2: */
/* 			if ( RF_MESSAGE_RECEIVED ) */
/* 			{ */
/* 				SecondTimeNumber = RF_Get_RemoteNumber(); */
/* 				Timeout = 8; */
				
/* 				if ( FirstTimeNumber == SecondTimeNumber ) */
/* 				{ */
/* 					MemCpy((unsigned char*)&Record_RF_Remote, (unsigned char*)FirstTimeNumber, 4); /\* Clear Record_RF_Remote *\/ */

/* 					if ( RF_LearnParameters == RF_REMOTE_TRANSMIT ) */
/* 					{	 */
/* 						FirstTimeNumber = 0; */
/* 						MemCpy((unsigned char*)&Record_RF_Remote, RF_Message, 3); /\* ID + Button *\/ */
/* 						Record_RF_Remote.Special.Transmit = TRUE; */
/* 						//Record_RF_Remote. */
/* 						RF_LearnParameters = 0; */
/* 					} */
/* 					else */
/* 					{ */
/* 						MemCpy((unsigned char*)&Record_RF_Remote.ID, RF_Message, 2); /\* ID only *\/ */
/* 					} */

/* 					Address = Ex_Entry_Write(LIST_RF_REMOTE, (unsigned char*)&Record_RF_Remote); */
/* 					if ( Address == EX_ERROR ) */
/* 					{ */
/* 						Debug("\r\t\t>> ERROR #%d", Ex_Errno ); */
/* 					} */
/* 					else */
/* 					{ */
/* 						Debug("\r\t\t>>> Learn - OK; EEPROM address: 0x%X", Address ); */
/* 					} */
/* 				} */
/* 				else */
/* 				{ */
/* 					putsf("\r\t\t>> ERROR: is not the SAME BUTTON\r \t\tTry again.\r", AUX_PORT); */
/* 				} */

/* 				State = 0; */
/* 			} */
/* 			break; */

/* 		default: */
/* 			; */
/* 	} */

/* }  */



/* 					if ( Received_RF_Remote.Button == RF_REMOTE_BUTTON_ARM ) /\* ARM button *\/ */
/* 					{ */
/* 						SysState = STATE_ARM; */
/* 						putsf("\r State: ARM", AUX_PORT); */
/* 						State = 0; */
/* 					} */
/* 					else */
/* 					{ */
/* 						State++; */
/* 					} */

